package com.dashu.device.service;

import com.dashu.base.utils.date.TimeUtils;
import com.dashu.device.modelat.Distinguish;
import org.hibernate.Session;
import org.springframework.stereotype.Service;

import javax.persistence.Query;
import javax.transaction.Transactional;
import java.sql.*;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Set;

@Service
public class AtService extends DataService {

    private String tableName = TimeUtils.getNowTimeDate();

    @Transactional
    public boolean save(Distinguish distinguish) {
        boolean flag = false;
        try {
            Session s = (Session) entityManager1.getDelegate();
            s.doWork(connection -> {
                PreparedStatement stmt = connection.prepareStatement("INSERT INTO `" + tableName + "` VALUES ("
                        + distinguish.getId() + ", '"
                        + distinguish.getName() + "', '"
                        + distinguish.getIden() + "', "
                        + distinguish.getCId() + ", '"
                        + distinguish.getImg() + "', '"
                        + distinguish.getIcon() + "', '"
                        + distinguish.getUuid() + "', "
                        + distinguish.getCaptureTime() + ", "
                        + distinguish.getCompareResult() + ", "
                        + distinguish.getTemperature() + ", "
                        + distinguish.getCompanyId() + ", "
                        + distinguish.getDepartmentId() + ", "
                        + distinguish.getClsId() + ", "
                        + distinguish.getAttendanceId() + ", '"
                        + distinguish.getPersonalType() + "', '"
                        + distinguish.getNum() + "', "
                        + distinguish.getTeacherPhone() + ", "
                        + distinguish.getAttendanceNum() + ",'"
                        + distinguish.getTeacherType() + "', '"
                        + distinguish.getStatus() + "', '"
                        + distinguish.getInAndOut()+ "');");
                stmt.execute();
            });
            flag = true;
        } catch (Exception e) {
            System.out.println("---------------保存出错---------------");
            throw e;
        }
        return flag;
    }

    @Transactional
    public Object findByid(Object o, Long id) {
        return entityManager1.find(o.getClass(), id);
    }

    @Transactional
    public List<Object> findBysql(String filed, Object o) {
        String sql = "from " + tableName + " WHERE " + filed + "=?";
        System.out.println(sql + "--------sql语句-------------");
        Query query = entityManager1.createQuery(sql);
        query.setParameter(1, o);
        List<Object> list = query.getResultList();
        entityManager1.close();
        return list;
    }

    @Transactional
    public List<Object> findpages(String filed, Object o, int start, int pageNumer) {
        String sql = "from " + tableName + " u WHERE u." + filed + "=?";
        System.out.println(sql + "--------page--sql语句-------------");
        List<Object> list = new ArrayList<>();
        try {
            Query query = entityManager1.createQuery(sql);
            query.setParameter(1, o);
            query.setFirstResult((start - 1) * pageNumer);
            query.setMaxResults(pageNumer);
            list = query.getResultList();
            entityManager1.close();
        } catch (Exception e) {
            System.out.println("------------分页错误---------------");
        }

        return list;
    }


    @Transactional
    public boolean update(Object entity) {
        boolean flag = false;
        try {
            entityManager1.merge(entity);
            flag = true;
        } catch (Exception e) {
            System.out.println("---------------更新出错---------------");
        }
        return flag;
    }

    @Transactional
    public Integer updateMoreFiled(LinkedHashMap<String, Object> map) {
        String sql = "UPDATE " + tableName + " AS u SET ";
        Set<String> set = null;
        set = map.keySet();
        List<String> list = new ArrayList<>(set);
        for (int i = 0; i < list.size() - 1; i++) {
            if (map.get(list.get(i)).getClass().getTypeName() == "java.lang.String") {
                System.out.println("-*****" + map.get(list.get(i)) + "------------" + list.get(i));
                sql += "u." + list.get(i) + "='" + map.get(list.get(i)) + "' , ";
            } else {
                sql += "u." + list.get(i) + "=" + map.get(list.get(i)) + " , ";
            }
        }
        sql = sql.substring(0, sql.length() - 2);
        sql += "where u.id=? ";
        System.out.println(sql + "--------sql语句-------------");
        int resurlt = 0;
        try {
            Query query = entityManager1.createQuery(sql);
            query.setParameter(1, map.get("id"));
            resurlt = query.executeUpdate();
        } catch (Exception e) {
            System.out.println("更新出错-----------------------");
            e.printStackTrace();

        }
        return resurlt;
    }


    @Transactional
    public boolean delete(Object entity) {
        boolean flag = false;
        try {
            entityManager1.remove(entityManager1.merge(entity));
            flag = true;
        } catch (Exception e) {
            System.out.println("---------------删除出错---------------");
        }
        return flag;
    }

    public Object findCount(LinkedHashMap<String, Object> map) {
        String sql = "select count(u) from " + tableName + " u WHERE ";
        Set<String> set = null;
        set = map.keySet();
        List<String> list = new ArrayList<>(set);
        List<Object> filedlist = new ArrayList<>();
        for (String filed : list) {
            sql += "u." + filed + "=? and ";
            filedlist.add(filed);
        }
        sql = sql.substring(0, sql.length() - 4);
        System.out.println(sql + "--------sql语句-------------");
        Query query = entityManager1.createQuery(sql);
        for (int i = 0; i < filedlist.size(); i++) {
            query.setParameter(i + 1, map.get(filedlist.get(i)));
        }
        return query.getSingleResult();
    }


}
